<?php
/**
 * User: 魔十七
 * Date: 2020/11/5
 * Time: 17:40
 */
//获取图片地址
function getPic($pic = ''){

        if(strpos($pic,"http") !== false){
            return $pic;
        }else {
            return "https://".$_SERVER['SERVER_NAME'].$pic;
        }
}
/**
 * 获取栏目列表
 * @param int $parent_id 上级ID
 * @param int $cat_type  后台导航
 * @param int $scene     0:后台 1:前台
 * @param int $all_cat   0:只调用1级 1:递归所有
 * @return array
 * @throws \think\db\exception\DataNotFoundException
 * @throws \think\db\exception\ModelNotFoundException
 * @throws \think\exception\DbException
 */
function getCat()
{
    $table='merchant_menu';
        $list = S($table);
//        if(empty($list)){
            $list = M('merchant_menu')->where(['is_show'=>1])->order('sequence desc')->select();
//            S($table,$list);
//        }
//    S($table,null);
    return newCat($list);
}
/**
 * 重构分类列表
 * @param array $list
 * @param int $parent_id
 * @param int $scene
 * @param int $all_cat
 * @return array
 */
function newCat($list = [],$parent_id = 0, $scene = 0, $all_cat = 1){
    static $n_list;
    if($all_cat == 0) $n_list = [];
    foreach ($list as $key => $value){
        if (($scene == 0 && $value['parent_id'] == $parent_id) || ($scene == 1 && $value['parent_id'] == $parent_id && $value['is_show'] == 1)) {
            if(array_key_exists('pic', $value)) $value['pic'] =getPic($value['pic']);
            $n_list[] = $value;
            if($all_cat) newCat($list,$value['id'],$scene);
        }
    }
    return $n_list;
}